﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using SysFrame.BP;
using SysFrame.Message;

namespace AppServer.Controllers.System
{
    [Route("api/[controller]")]
    [ApiController]
    public class System1Controller : SysControllerBase
    {
        //功能
        // POST: api/System6
        [HttpPost]
        public ActionResult<string> Post([FromBody] dynamic data)
        {
            string jsTimeStamp = data.jsTimeStamp;
            string Action = data.Action == null ? "Get" : data.Action;
            string rtstr = "";
            //得到列表
            if (Action == "Get")
            {
                rtstr = Get(data);
            }
            if (Action == "Save")
            {
                rtstr = Save(data);
            }
            if (Action == "Login")
            {
                rtstr = Login(data);
            }
            if (Action == "GetBtnPermission")
            {
                rtstr = GetBtnPermission(data);
            }
            return rtstr;
        }


        /// <summary>
        /// 得到用户列表
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Get(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            string search = postdata["search"].ToString();
            string notuid = postdata["notuid"].ToString();
            BLLsystem_1 bll = new BLLsystem_1();
            List<Mssystem_1> mssys1 = new List<Mssystem_1>();
            MsQuery msq = new MsQuery();
            if (!string.IsNullOrEmpty(search))
            {
                msq.strquery += " and (System_1_20 like '%" + search + "%' ";
                msq.strquery += " or System_1_40 like '%" + search + "%' ";
                msq.strquery += " or System_1_120 like '%" + search + "%' ";
                msq.strquery += " or System_1_140 like '%" + search + "%' ";
                msq.strquery += " )"; 
            }
            if (string.IsNullOrEmpty(notuid))
            {
                notuid = "-1";
            }
            notuid += ",1";
            msq.strquery += " and SYSTEM_1_10 not in (" + notuid + ") ";
            mssys1 = bll.Getsystem_1MsByQuery(msq);
            return JArray.FromObject(mssys1).ToString();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Save(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            string userdata = postdata["data"].ToString();
            Mssystem_1 ms = Mssystem_1.parse(userdata);
            string res = "";
            MsReturned msr = new MsReturned();
            msr = new BLLsystem_1().Savesystem_1(ms);
            res = JObject.FromObject(msr).ToString();
            return res;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Login(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            string username = postdata["username"].ToString();
            string psw = postdata["psw"].ToString();
            string res = "";
            MsReturned msr = new MsReturned();
            if (string.IsNullOrEmpty(username))
            {
                res = "请输入用户名";
                return res;
            }
            if (string.IsNullOrEmpty(psw))
            {
                res = "请输入密码";
                return res;
            }
            BLLsystem_1 bll = new BLLsystem_1();
            MsReturned ms = new MsReturned();
            ms = bll.UserLode(username, psw);
            switch (ms.strMsId)
            {
                case "-1":
                    res = "用户名不存在";
                    break;
                case "-2":
                    res = "用户名或密码错误";
                    break;
                case "1":
                    msr.strMsId = "1";
                    string sys1 = JObject.FromObject((Mssystem_1)ms.obj1).ToString();
                    msr.data = EncryptDES.strEncryptToDES(sys1);
                    res = "登录成功";
                    break;
                default:
                    break;
            }
            msr.strMS = res;
            return JObject.FromObject(msr).ToString();
        }

        /// <summary>
        /// 得到按钮
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string GetBtnPermission(dynamic data)
        {
            JObject postdata = JObject.FromObject(data.data);
            string MenuId = postdata["MenuId"].ToString();
            Mssystem_1 lodeuser = GetSession<Mssystem_1>("LoginUser");
            List<string> btnids = BLLsystem_1.GetUserBtnByMenu(lodeuser.sys10.System_10_10, MenuId, true);
            if (lodeuser.isadmin) {
                btnids = BLLsystem_1.GetAllBtnByMenu(MenuId);
            }
            string res = JArray.FromObject(btnids).ToString(); ;
            return res;
        }
    }
}